/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.frame;

import static com.vertexnet.realestate.constants.UIConstants.AGENT;
import static com.vertexnet.realestate.constants.UIConstants.CUSTOMER;
import static com.vertexnet.realestate.constants.UIConstants.EMPLOYEE;
import static com.vertexnet.realestate.constants.UIConstants.LAYOUT;
import static com.vertexnet.realestate.constants.UIConstants.PAYMENT_PLAN;

import java.awt.Component;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.vertexnet.realestate.interfaces.Controller;
import com.vertexnet.realestate.interfaces.DefaultPanel;
import com.vertexnet.realestate.interfaces.MainView;
import com.vertexnet.realestate.model.tablemodel.AgentsTableModel;
import com.vertexnet.realestate.model.tablemodel.CustomersTableModel;
import com.vertexnet.realestate.model.tablemodel.EmployeesTableModel;
import com.vertexnet.realestate.model.tablemodel.LayoutsTableModel;
import com.vertexnet.realestate.model.tablemodel.PaymentPlansTableModel;
import com.vertexnet.realestate.view.panel.AdminPanel;
import com.vertexnet.realestate.view.panel.ReportsPanel;
import com.vertexnet.realestate.view.panel.factory.PanelGenerator;
import com.vertexnet.realestatenet.model.bean.Agent;
import com.vertexnet.realestatenet.model.bean.Area;
import com.vertexnet.realestatenet.model.bean.Customer;
import com.vertexnet.realestatenet.model.bean.Employee;
import com.vertexnet.realestatenet.model.bean.Layout;
import com.vertexnet.realestatenet.model.bean.PaymentPlan;
import com.vertexnet.realestatenet.model.bean.ReportBean;
import com.vertexnet.realestatenet.model.bean.SecurityQuestion;

/**
 *
 * @author Srivathsa
 */
public class RealEstateFrame extends javax.swing.JFrame implements MainView {

	private static final long serialVersionUID = 1L;
	private Controller controller;
	
	private List<Customer> customers;
	private List<Layout> layouts;
	private List<Agent> agents;
	private List<PaymentPlan> paymentPlans;
	private List<Employee> employees;
	
	private List<Customer> selectedCustomers;
	private List<Layout> selectedLayouts;
	private List<Agent> selectedAgents;
	private List<PaymentPlan> selectedPaymentPlans;
	private List<Employee> selectedEmployees;
	
	private DefaultPanel customersPanel;
	private DefaultPanel layoutsPanel;
	private DefaultPanel agentsPanel;
	private DefaultPanel paymentPlansPanel;
	private DefaultPanel employeesPanel;
	private DefaultPanel reportsPanel;
	private DefaultPanel adminPanel;
	
	
	/**
     * Creates new form RealEstateFrame
     */
    public RealEstateFrame(Controller controller) {
        initComponents();
        this.controller = controller;
      
        customersPanel = PanelGenerator.createCustomersPanel(this, controller);
        layoutsPanel = PanelGenerator.createLayoutsPanel(this, controller);
        paymentPlansPanel = PanelGenerator.createPaymentPlanPanel(this, controller);
        agentsPanel = PanelGenerator.createAgentsPanel(this, controller);
        employeesPanel = PanelGenerator.createEmployeesPanel(this, controller);
        reportsPanel = PanelGenerator.createReportsPanel(this, controller);
        adminPanel = PanelGenerator.createAdminPanel(this, controller);
        
        realEstateTabbedPane.addTab("Customers", (Component) customersPanel);
        realEstateTabbedPane.addTab("Layouts", (Component) layoutsPanel);
        realEstateTabbedPane.addTab("Payment Plans", (Component) paymentPlansPanel);
        realEstateTabbedPane.addTab("Agents", (Component) agentsPanel);
        //realEstateTabbedPane.addTab("Employees", (Component) employeesPanel);
        realEstateTabbedPane.addTab("Reports", (Component) reportsPanel);
        realEstateTabbedPane.addTab("Administration", (Component) adminPanel);
        
        settingMenu.setVisible(false);
        helpMenu.setVisible(false);
        newEmployeeMenuItem.setVisible(false);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenu3 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        realEstateTabbedPane = new javax.swing.JTabbedPane();
        realEstateMenuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newCustomerMenuItem = new javax.swing.JMenuItem();
        newLayoutMenuItem = new javax.swing.JMenuItem();
        newPaymentPlanMenuItem = new javax.swing.JMenuItem();
        newAgentMenuItem = new javax.swing.JMenuItem();
        newEmployeeMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        settingMenu = new javax.swing.JMenu();
        userSettingsMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        jMenu3.setText("jMenu3");

        jMenu2.setText("jMenu2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        fileMenu.setText("File");

        newCustomerMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newCustomerMenuItem.setText("New Customer");
        newCustomerMenuItem.setName(CUSTOMER.getAdd());
        newCustomerMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newCustomerMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newCustomerMenuItem);

        newLayoutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        newLayoutMenuItem.setText("New Layout");
        newLayoutMenuItem.setName(LAYOUT.getAdd());
        newLayoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newLayoutMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newLayoutMenuItem);

        newPaymentPlanMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        newPaymentPlanMenuItem.setText("New Payment Plan");
        newPaymentPlanMenuItem.setName(PAYMENT_PLAN.getAdd());
        newPaymentPlanMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPaymentPlanMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newPaymentPlanMenuItem);

        newAgentMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        newAgentMenuItem.setText("New Agent");
        newAgentMenuItem.setName(AGENT.getAdd());
        newAgentMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newAgentMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newAgentMenuItem);

        newEmployeeMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        newEmployeeMenuItem.setText("New Employee");
        newEmployeeMenuItem.setName(EMPLOYEE.getAdd());
        newEmployeeMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newEmployeeMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newEmployeeMenuItem);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitMenuItem);

        realEstateMenuBar.add(fileMenu);

        settingMenu.setText("Settings");

        userSettingsMenuItem.setText("User Settings");
        userSettingsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userSettingsMenuItemActionPerformed(evt);
            }
        });
        settingMenu.add(userSettingsMenuItem);

        realEstateMenuBar.add(settingMenu);

        helpMenu.setText("Help");

        jMenuItem1.setText("Help Contents");
        helpMenu.add(jMenuItem1);

        jMenuItem2.setText("About");
        helpMenu.add(jMenuItem2);

        realEstateMenuBar.add(helpMenu);

        setJMenuBar(realEstateMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(realEstateTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 975, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(realEstateTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cutMenuItemActionPerformed

    }//GEN-LAST:event_cutMenuItemActionPerformed

    private void copyMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyMenuItemActionPerformed

    }//GEN-LAST:event_copyMenuItemActionPerformed

    private void pasteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteMenuItemActionPerformed

    }//GEN-LAST:event_pasteMenuItemActionPerformed

    private void newLayoutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newLayoutMenuItemActionPerformed
		controller.handleEvent(evt);
    }//GEN-LAST:event_newLayoutMenuItemActionPerformed

    private void newPaymentPlanMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPaymentPlanMenuItemActionPerformed
		controller.handleEvent(evt);
    }//GEN-LAST:event_newPaymentPlanMenuItemActionPerformed

    private void newAgentMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newAgentMenuItemActionPerformed
		controller.handleEvent(evt);
    }//GEN-LAST:event_newAgentMenuItemActionPerformed

    private void newEmployeeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newEmployeeMenuItemActionPerformed
		controller.handleEvent(evt);
    }//GEN-LAST:event_newEmployeeMenuItemActionPerformed

    private void userSettingsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userSettingsMenuItemActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_userSettingsMenuItemActionPerformed

	private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_exitMenuItemActionPerformed
		this.dispose();
		System.exit(0);
	}// GEN-LAST:event_exitMenuItemActionPerformed

	private void newCustomerMenuItemActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_AddCustomerMenuItemActionPerformed
		controller.handleEvent(evt);
	}// GEN-LAST:event_AddCustomerMenuItem;ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem newAgentMenuItem;
    private javax.swing.JMenuItem newCustomerMenuItem;
    private javax.swing.JMenuItem newEmployeeMenuItem;
    private javax.swing.JMenuItem newLayoutMenuItem;
    private javax.swing.JMenuItem newPaymentPlanMenuItem;
    private javax.swing.JMenuBar realEstateMenuBar;
    private javax.swing.JTabbedPane realEstateTabbedPane;
    private javax.swing.JMenu settingMenu;
    private javax.swing.JMenuItem userSettingsMenuItem;
    // End of variables declaration//GEN-END:variables


    public void setController(Controller controller) {
		this.controller = controller;
	}

	public List<Customer> getSelectedCustomers() {
		return selectedCustomers;
	}

	public List<Layout> getSelectedLayouts() {
		return selectedLayouts;
	}

	public List<PaymentPlan> getSelectedPaymentPlans() {
		return selectedPaymentPlans;
	}

	public List<Employee> getSelectedEmployees() {
		return selectedEmployees;
	}

	public List<Agent> getSelectedAgents() {
		return selectedAgents;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
		((CustomersTableModel) customersPanel.getTable().getModel()).setObjects(customers);
		updateCustomers();
	}

	public void setLayouts(List<Layout> layouts) {
		this.layouts = layouts;
		((LayoutsTableModel)layoutsPanel.getTable().getModel()).setObjects(layouts);
		updateLayouts();
	}
	
	public void setAgents(List<Agent> agents) {
		this.agents = agents;
		((AgentsTableModel)agentsPanel.getTable().getModel()).setObjects(agents);
		updateAgents();
	}

	public void setPaymentPlans(List<PaymentPlan> paymentPlans) {
		this.paymentPlans = paymentPlans;
		((PaymentPlansTableModel) paymentPlansPanel.getTable().getModel()).setObjects(paymentPlans);
		updatePaymentPlans();
	}
	
	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
		((EmployeesTableModel) employeesPanel.getTable().getModel()).setObjects(employees);
		updateEmployees();
	}
	
	public void updateCustomers() {
		((CustomersTableModel) customersPanel.getTable().getModel()).fireTableDataChanged();
	}

	public void updateLayouts() {
		((LayoutsTableModel)layoutsPanel.getTable().getModel()).fireTableDataChanged();
	}

	public void updatePaymentPlans() {
		((PaymentPlansTableModel) paymentPlansPanel.getTable().getModel()).fireTableDataChanged();
	}

	public void updateAgents() {
		((AgentsTableModel)agentsPanel.getTable().getModel()).fireTableDataChanged();
	}

	public void updateEmployees() {
		((EmployeesTableModel) employeesPanel.getTable().getModel()).fireTableDataChanged();
	}

	public void setSelectedCustomers(List<Customer> selectedCustomers) {
		this.selectedCustomers = selectedCustomers;
	}

	public void setSelectedLayouts(List<Layout> selectedLayouts) {
		this.selectedLayouts = selectedLayouts;
	}

	public void setSelectedPaymentPlans(List<PaymentPlan> selectedPaymentPlans) {
		this.selectedPaymentPlans = selectedPaymentPlans;
	}

	public void setSelectedAgents(List<Agent> selectedAgents) {
		this.selectedAgents = selectedAgents;
	}

	public void setSelectedEmployees(List<Employee> selectedEmployees) {
		this.selectedEmployees = selectedEmployees;
	}

	public void showSelectObjectEditDialog(String name) {
		JOptionPane.showMessageDialog(this, "Please Select Single " + name + " to Edit");
	}

	public void showSelectObjectDeleteDialog(String name) {
		JOptionPane.showMessageDialog(this, "Please Select Atlest one " + name + " to Delete");
	}

	public void showSelectObjectReportDialog(String name) {
		JOptionPane.showMessageDialog(this, "Please Select Atlest one " + name + " to Generate Report");
	}

	public List<Area> getAreasToAdd() {
		AdminPanel adminPanel = (AdminPanel) this.adminPanel;
		return adminPanel.getAreasToAdd();
	}

	public List<Area> getAreasToDelete() {
		AdminPanel adminPanel = (AdminPanel) this.adminPanel;
		return adminPanel.getAreasToDelete();
	}

	public List<SecurityQuestion> getSecurityQuestionsToAdd() {
		AdminPanel adminPanel = (AdminPanel) this.adminPanel;
		return adminPanel.getSecurityQuestionsToAdd();
	}

	public List<SecurityQuestion> getSecurityQuestionsToDelete() {
		AdminPanel adminPanel = (AdminPanel) this.adminPanel;
		return adminPanel.getSecurityQuestionsToDelete();
	}

	public void setAreas(List<Area> areas) {
		((AdminPanel) adminPanel).setAreas(areas);
	}

	public void setSecurityQuestions(List<SecurityQuestion> securityQuestions) {
		((AdminPanel) adminPanel).setSecurityQuestions(securityQuestions);
	}


	public ReportBean getReportBean() {
		return ((ReportsPanel)reportsPanel).getReportBean();
	}
}
